/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-2
 * @Time  : 下午8:11
 * @brief :
 */
exports.code =function(phone,id){
    var Dialog = require('common:widget/ui/dialog/dialog.js').Dialog,
        $widget = $('.widget-code'),
        $messageBtn = $widget.find('.btn-message'),
        $voice = $widget.find('.voice'),
        dialog = new Dialog();

    function checkPhone(){
        if(!phone || !/^\d{11}$/.test(phone)){
            dialog.show('手机号码输入错误');
            return false;
        }
        return true;
    }

    function sendCode(type,callback){
        $.post('/account/sendidentitycodenew',{
            'mobile':phone,
            'type':type
        },function(ret,status) {
            if(ret.error_no == 0){
                callback();
            }else{
                dialog.show(ret.error_message);
                $messageBtn.removeClass('btn-disable').addClass('btn-blank1').text('短信获取');
            }
        },'json');
    }


    function intervalCount(){
        var count = 60,
            countInterval = setInterval(function(){
            $messageBtn.text(count+'秒后重新获取');
            if(count>0){
                count--;
            }else{
                count = 60;
                clearInterval(countInterval);
                $voice.slideDown();
                $messageBtn.removeClass('btn-disable').addClass('btn-blank1').text('短信获取');
            }
        },1000);
    }


    $messageBtn.click(function(){
        if(checkPhone()){
            var me = this;
            if($(this).hasClass('btn-blank1')){
                $(this).removeClass('btn-blank1').addClass('btn-disable');
                $voice.hide();
                sendCode(0,function(){
                    intervalCount();
                });
            }
        }

    });

    $voice.find('.btn-voice').click(function(e){
        e.stopPropagation();
        if(checkPhone()){
            e.stopPropagation();
            $messageBtn.removeClass('btn-blank1').addClass('btn-disable');
            $voice.hide();
            sendCode(1,function(){
                intervalCount();
            });
        }
    });

    $('body').click(function(){
        $voice.hide();
    });

};